package com.ssm.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.springboot.dao.QfCourseClassifyDao;
import com.ssm.springboot.dao.QfCourseDao;
import com.ssm.springboot.dao.QfTeachDao;
import com.ssm.springboot.pojo.*;
import com.ssm.springboot.service.QfCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * (QfCourse)表服务实现类
 *
 * @author makejava
 * @since 2021-12-23 15:06:00
 */
@Service
public class QfCourseServiceImpl implements QfCourseService {
    @Resource
    private QfCourseDao qfCourseDao;

    @Resource
    private QfCourseClassifyDao qfCourseClassifyDao;

    @Autowired
    private QfTeachDao qfTeachDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public DataInfo queryById(Integer id) {
        Course qfCourse = qfCourseDao.queryById(id);
        return new DataInfo(0,"查询成功",0L,qfCourse);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     * @param page
     * @param limit
     */
    @Override
    public DataInfo queryAllByLimit(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Course> courses = qfCourseDao.queryAllByLimit();
        PageInfo<Course> coursePageInfo = PageInfo.of(courses);

        return new DataInfo(0,"查询成功",coursePageInfo.getTotal(),coursePageInfo.getList());
    }

    /**
     * 新增数据
     *
     * @param qfCourse 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public DataInfo insert(Course qfCourse) {
        qfCourse.setCourseStatus("1");
        qfCourse.setCreatetime(new Timestamp(System.currentTimeMillis()));
        qfCourseDao.insert(qfCourse);
        qfCourseClassifyDao.insert(new QfCourseClassify(
                qfCourse.getId(),
                qfCourse.getCourseClassifyName()));
        return new DataInfo(0,"添加成功",0L,null);
    }

    /**
     * 修改数据
     *
     * @param qfCourse 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public DataInfo update(Course qfCourse) {
        qfCourse.setUpdatetime(new Timestamp(System.currentTimeMillis()));
        qfCourseDao.update(qfCourse);
        qfTeachDao.updateImg(Integer.parseInt(qfCourse.getTeachName()),qfCourse.getTeachImage());
        return new DataInfo(0,"更新成功",0L,null);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public DataInfo deleteById(Integer id) {

        //删除qfCourseDao表数据
        qfCourseDao.deleteById(id);
        //删除中间表数据
        qfCourseClassifyDao.deleteByCourseId(id);

        return new DataInfo(0,"删除成功",0L,null);


    }
    //通过课程类型,查新课程列表
    @Override
    public DataInfo selectByClassify(int id,int page,int limit) {
        PageHelper.startPage(page,limit);
        List<Course> courses = qfCourseDao.selectByClassify(id);
        PageInfo<Course> of = PageInfo.of(courses);
        return new DataInfo(0,"查询成功",of.getTotal(),of.getList());
    }
    //通过教师,查新课程列表
    @Override
    public DataInfo selectByTeach(int id, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Course> courses =qfCourseDao.selectByTeach(id);
        PageInfo<Course> of = PageInfo.of(courses);
        return new DataInfo(0,"查询成功",of.getTotal(),of.getList());
    }
}